package com.hnchances.controller;


import com.hnchances.config.JwtConfig;
import com.hnchances.entity.Student;
import com.hnchances.entity.User;
import com.hnchances.service.StudentService;
import com.hnchances.service.UserService;
import com.hnchances.utils.IpUtil;
import com.hnchances.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.SocketException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyp
 *
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理相关接口")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private StudentService studentService;
    @Resource
    private JwtConfig jwtConfig;

    /**
     * 登录
     * 地址：localhost:8000/user/login
     * 参数：
     * {
     *     "username":"stu001",
     *     "password":"123456"
     * }
     * @param user
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 注册
     * 地址：localhost:8000/user/register
     * 学生注册参数格式案例：
     * {
     *     "username":"stu001",
     *     "password":"123456",
     *     "phone":"19823728888",
     *     "truename":"张三",
     *     "identity":0,
     *     "idcard":"220284200109096217"
     * }
     *
     * 教师注册阐述格式案列：
     * {
     *     "username":"tea001",
     *     "password":"123456",
     *     "phone":"18343246781",
     *     "truename":"王群",
     *     "identity":1,
     *     "courseid":1,
     *     "classid":1,
     *     "code":"34851"
     * }
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        return userService.register(user);
    }

    /**
     * 请求地址：localhost:8000/user/getCode/18343246781
     * @param phone
     * @return
     */
    @GetMapping("/getCode/{phone}")
    public Result getCode(@PathVariable("phone") String phone){
        return userService.getCode(phone);
    }

    /**
     * 修改用户
     * 地址：localhost:8000/user/update
     * 请求头：Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjU5OTQ5NzE2LCJleHAiOjE2NTk5NTMzMTZ9.tQvTfq31wxd-seq95pIGCxiXp4sqcFahHMgM9LH3bPxMLWAIRCO5kdExNROEy9sYTID7EnUw9PnzB8XbfkvHtQ
     * 参数：
     * {
     *     "id":1,
     *     "phone":"18343246781",
     *     "truename":"张三丰"
     * }
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        return userService.update(user);
    }

    /**
     * 删除用户
     * 地址：localhost:8000/user/1
     * 请求头：Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjU5OTQ5NzE2LCJleHAiOjE2NTk5NTMzMTZ9.tQvTfq31wxd-seq95pIGCxiXp4sqcFahHMgM9LH3bPxMLWAIRCO5kdExNROEy9sYTID7EnUw9PnzB8XbfkvHtQ
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        if (dbUser.getIdentity()==0){
            return Result.error().message("您无权限删除信息");
        }else {
            return userService.delete(id);
        }
    }

    /**
     * 查询用户信息
     * 请求头：Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjU5OTQ5NzE2LCJleHAiOjE2NTk5NTMzMTZ9.tQvTfq31wxd-seq95pIGCxiXp4sqcFahHMgM9LH3bPxMLWAIRCO5kdExNROEy9sYTID7EnUw9PnzB8XbfkvHtQ
     * @param request
     * @return
     */
    @GetMapping("/userinfo")
    public String userinfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        return dbUser.toStringInfo();
    }
}

